﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.VisualBasic;
using System.Text.RegularExpressions;

namespace Bing.Toolkit.Utility
{
    /// <summary>
    /// 常用字符串处理辅助类
    /// </summary>
    public static class StringHelper
    {
        public class StrEncoding
        {
            public static byte[] StrToBytes(string input)
            {
                return StrToBytes(input, Encoding.UTF8);
            }

            public static byte[] StrToBytes(string input, string encoding)
            {
                Encoding e = Encoding.GetEncoding(encoding);
                return StrToBytes(input, e);
            }

            public static byte[] StrToBytes(string input, Encoding encoding)
            {
                return encoding.GetBytes(input);
            }

            public static string BytesToStr(byte[] input)
            {
                return BytesToStr(input, Encoding.UTF8);
            }

            public static string BytesToStr(byte[] input, string encoding)
            {
                Encoding e = Encoding.GetEncoding(encoding);
                return BytesToStr(input, e);
            }

            public static string BytesToStr(byte[] input, Encoding encoding)
            {
                return encoding.GetString(input);
            }
        }
        ///// <summary>
        ///// 转换
        ///// </summary>
        //public class StrConv
        //{
        //    /// <summary>
        //    /// 全角转半角
        //    /// </summary>
        //    /// <param name="input">要转换的字符串</param>
        //    /// <returns>转换完的字符串</returns>
        //    public static string Narrow(string input)
        //    {
        //        return Strings.StrConv(input, VbStrConv.Narrow, 0);
        //    }
        //    /// <summary>
        //    /// 半角转全角
        //    /// </summary>
        //    /// <param name="input">要转换的字符串</param>
        //    /// <returns>转换完的字符串</returns>
        //    public static string Wide(string input)
        //    {
        //        return Strings.StrConv(input, VbStrConv.Wide, 0);
        //    }
        //    /// <summary>
        //    /// 简体转繁体
        //    /// </summary>
        //    /// <param name="input"></param>
        //    /// <returns></returns>
        //    public static string TraditionalChinese(string input)
        //    {
        //        return Strings.StrConv(input, VbStrConv.TraditionalChinese, 0);
        //    }
        //    /// <summary>
        //    /// 繁体转简体
        //    /// </summary>
        //    /// <param name="input"></param>
        //    /// <returns></returns>
        //    public static string SimplifiedChinese(string input)
        //    {
        //        return Strings.StrConv(input, VbStrConv.SimplifiedChinese, 0);
        //    }
        //    /// <summary>
        //    /// 將字串中每個字的第一個字母轉換為大寫
        //    /// </summary>
        //    /// <param name="input"></param>
        //    /// <returns></returns>
        //    public static string ProperCase(string input)
        //    {
        //        return Strings.StrConv(input, VbStrConv.ProperCase, 0);
        //    }
        //}
        /// <summary>
        /// 字符串截取
        /// </summary>
        /// <param name="val">要截取的字符串</param>
        /// <param name="length">长度</param>
        /// <returns>结果</returns>
        /// <example><![CDATA[StringHelper.SubString("我是小强", 10);]]></example>
        public static string SubString(string val, int length)
        {
            if (val == null || val.Length == 0 || length <= 0)
            {
                return string.Empty;
            }

            int len = Encoding.Default.GetByteCount(val);

            if (len <= length)
            {
                return val;
            }

            char[] t = val.ToCharArray();

            StringBuilder result = new StringBuilder();

            int i = 0, j = 0;

            while (i < length)
            {
                i += Encoding.Default.GetByteCount(t[j].ToString());
                result.Append(t[j]);
                j++;
            }

            if (i > length)
            {
                result.Remove(0, result.Length - 1);
            }
            return result.ToString();
        }
        /// <summary>
        /// 去除HTML
        /// </summary>
        /// <param name="val">字符串</param>
        /// <returns>结果</returns>
        /// <example><![CDATA[StringHelper.Html("<a>哈a哈a哈哈</a><br/><br></br><font>bbbbb</font>") ]]></example>
        public static string Html(string val)
        {
            return Regex.Replace(val, "<[^>]*>", string.Empty, RegexOptions.IgnoreCase);
        }
        /// <summary>
        /// 去除HTML
        /// </summary>
        /// <param name="val">字符串</param>
        /// <param name="tag">标记</param>
        /// <returns>结果</returns>
        /// <example><![CDATA["<a>哈a哈a哈哈</a><br/><br></br><font>bbbbb</font>".Html("br") ]]></example>
        public static string Html(string val, string tag)
        {
            return Regex.Replace(val, @"<[/" + tag + @"\s/]*>", string.Empty);
        }
        /// <summary>
        /// 文本注入防范,主要去除script,iframe
        /// </summary>
        /// <param name="val">字符串</param>
        /// <returns>结果</returns>
        /// <example><![CDATA[StringHelper.TextInjection("<a>哈a哈a哈哈</a><br/><br></br><script>var a ='';</script><font>bbbbb</font>") ]]></example>
        public static string TextInjection(string val)
        {
            return Regex.Replace(val, @"((<[\s\/]*[script\b[^>]*>)([^>]*)(<\/script>))|((<[\s\/]*[iframe\b[^>]*>)([^>]*)(<\/iframe>))", "", RegexOptions.IgnoreCase);
        }
        /// <summary>
        /// 返回标准IP
        /// </summary>
        /// <param name="ip">IP</param>
        /// <returns></returns>
        public static string CriterionIP(string ip)
        {
            if (string.IsNullOrEmpty(ip))
                throw new Exception("未输入任务要转换的字符串!");

            string[] s = ip.Split('.');
            StringBuilder result = new StringBuilder();

            foreach (string n in s)
            {
                result.Append(n.PadLeft(3, '0'));
                result.Append(".");
            }
            result.Remove(result.Length - 1, 1);

            return result.ToString();
        }

        /// <summary>
        /// 将字符串组合成路径
        /// </summary>
        /// <param name="paths"></param>
        /// <returns></returns>
        public static string PathCombine(params string[] paths)
        {
            if (paths.Length == 0)
            {
                throw new ArgumentException("please input path");
            }
            else
            {
                StringBuilder builder = new StringBuilder();
                string spliter = "\\";

                string firstPath = paths[0];

                if (firstPath.StartsWith("HTTP", StringComparison.OrdinalIgnoreCase))
                {
                    spliter = "/";
                }

                if (!firstPath.EndsWith(spliter))
                {
                    firstPath = firstPath + spliter;
                }
                builder.Append(firstPath);

                for (int i = 1; i < paths.Length; i++)
                {
                    string nextPath = paths[i];
                    if (nextPath.StartsWith("/") || nextPath.StartsWith("\\"))
                    {
                        nextPath = nextPath.Substring(1);
                    }

                    if (i != paths.Length - 1)//not the last one
                    {
                        if (nextPath.EndsWith("/") || nextPath.EndsWith("\\"))
                        {
                            nextPath = nextPath.Substring(0, nextPath.Length - 1) + spliter;
                        }
                        else
                        {
                            nextPath = nextPath + spliter;
                        }
                    }

                    builder.Append(nextPath);
                }

                return builder.ToString();
            }
        }
    }
}
